<!DOCTYPE html>
<html class="writer-html5" lang="en" >
<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ib_insync.wrapper &mdash; ib_insync 0.9.70 documentation</title>
      <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
      <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
    <link rel="canonical" href="https://ib_insync.readthedocs.io_modules/ib_insync/wrapper.html"/>
  
        <script data-url_root="../../" id="documentation_options" src="../../_static/documentation_options.js"></script>
        <script src="../../_static/jquery.js"></script>
        <script src="../../_static/underscore.js"></script>
        <script src="../../_static/doctools.js"></script>
    <script src="../../_static/js/theme.js"></script>
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav"> 
  <div class="wy-grid-for-nav">
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
            <a href="../../index.html" class="icon icon-home"> ib_insync
          </a>
              <div class="version">
                0.9
              </div>
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>
        </div><div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="Navigation menu">
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../readme.html">Introduction</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../api.html">API docs</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../notebooks.html">Notebooks</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../recipes.html">Code recipes</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../code.html">Source code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../changelog.html">Changelog</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../links.html">Links</a></li>
</ul>

        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap"><nav class="wy-nav-top" aria-label="Mobile navigation menu" >
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">ib_insync</a>
      </nav>

      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="Page navigation">
  <ul class="wy-breadcrumbs">
      <li><a href="../../index.html" class="icon icon-home"></a> &raquo;</li>
          <li><a href="../index.html">Module code</a> &raquo;</li>
      <li>ib_insync.wrapper</li>
      <li class="wy-breadcrumbs-aside">
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
             
  <h1>Source code for ib_insync.wrapper</h1><div class="highlight"><pre>
<span></span><span class="sd">&quot;&quot;&quot;Wrapper to handle incoming messages.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">asyncio</span>
<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">defaultdict</span>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">suppress</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span><span class="p">,</span> <span class="n">timezone</span>
<span class="kn">from</span> <span class="nn">typing</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">Any</span><span class="p">,</span> <span class="n">Dict</span><span class="p">,</span> <span class="n">List</span><span class="p">,</span> <span class="n">Optional</span><span class="p">,</span> <span class="n">Set</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">,</span> <span class="n">Union</span><span class="p">,</span> <span class="n">cast</span><span class="p">)</span>

<span class="kn">from</span> <span class="nn">ib_insync.contract</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">Contract</span><span class="p">,</span> <span class="n">ContractDescription</span><span class="p">,</span> <span class="n">ContractDetails</span><span class="p">,</span> <span class="n">DeltaNeutralContract</span><span class="p">,</span>
    <span class="n">ScanData</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">ib_insync.objects</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">AccountValue</span><span class="p">,</span> <span class="n">BarData</span><span class="p">,</span> <span class="n">BarDataList</span><span class="p">,</span> <span class="n">CommissionReport</span><span class="p">,</span>
    <span class="n">DOMLevel</span><span class="p">,</span> <span class="n">DepthMktDataDescription</span><span class="p">,</span>
    <span class="n">Dividends</span><span class="p">,</span> <span class="n">Execution</span><span class="p">,</span> <span class="n">Fill</span><span class="p">,</span> <span class="n">FundamentalRatios</span><span class="p">,</span> <span class="n">HistogramData</span><span class="p">,</span>
    <span class="n">HistoricalNews</span><span class="p">,</span> <span class="n">HistoricalTick</span><span class="p">,</span> <span class="n">HistoricalTickBidAsk</span><span class="p">,</span> <span class="n">HistoricalTickLast</span><span class="p">,</span>
    <span class="n">MktDepthData</span><span class="p">,</span> <span class="n">NewsArticle</span><span class="p">,</span> <span class="n">NewsBulletin</span><span class="p">,</span> <span class="n">NewsProvider</span><span class="p">,</span> <span class="n">NewsTick</span><span class="p">,</span>
    <span class="n">OptionChain</span><span class="p">,</span> <span class="n">OptionComputation</span><span class="p">,</span> <span class="n">PnL</span><span class="p">,</span> <span class="n">PnLSingle</span><span class="p">,</span> <span class="n">PortfolioItem</span><span class="p">,</span> <span class="n">Position</span><span class="p">,</span>
    <span class="n">PriceIncrement</span><span class="p">,</span> <span class="n">RealTimeBar</span><span class="p">,</span> <span class="n">RealTimeBarList</span><span class="p">,</span> <span class="n">TickAttribBidAsk</span><span class="p">,</span>
    <span class="n">TickAttribLast</span><span class="p">,</span> <span class="n">TickByTickAllLast</span><span class="p">,</span> <span class="n">TickByTickBidAsk</span><span class="p">,</span> <span class="n">TickByTickMidPoint</span><span class="p">,</span>
    <span class="n">TickData</span><span class="p">,</span> <span class="n">TradeLogEntry</span><span class="p">)</span>
<span class="kn">from</span> <span class="nn">ib_insync.order</span> <span class="kn">import</span> <span class="n">Order</span><span class="p">,</span> <span class="n">OrderState</span><span class="p">,</span> <span class="n">OrderStatus</span><span class="p">,</span> <span class="n">Trade</span>
<span class="kn">from</span> <span class="nn">ib_insync.ticker</span> <span class="kn">import</span> <span class="n">Ticker</span>
<span class="kn">from</span> <span class="nn">ib_insync.util</span> <span class="kn">import</span> <span class="p">(</span>
    <span class="n">UNSET_DOUBLE</span><span class="p">,</span> <span class="n">UNSET_INTEGER</span><span class="p">,</span> <span class="n">dataclassAsDict</span><span class="p">,</span> <span class="n">dataclassUpdate</span><span class="p">,</span>
    <span class="n">globalErrorEvent</span><span class="p">,</span> <span class="n">isNan</span><span class="p">,</span> <span class="n">parseIBDatetime</span><span class="p">)</span>


<span class="n">OrderKeyType</span> <span class="o">=</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Tuple</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span>


<div class="viewcode-block" id="RequestError"><a class="viewcode-back" href="../../api.html#ib_insync.wrapper.RequestError">[docs]</a><span class="k">class</span> <span class="nc">RequestError</span><span class="p">(</span><span class="ne">Exception</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Exception to raise when the API reports an error that can be tied to</span>
<span class="sd">    a single request.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">code</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">message</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Args:</span>
<span class="sd">          reqId: Original request ID.</span>
<span class="sd">          code: Original error code.</span>
<span class="sd">          message: Original error message.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;API error: </span><span class="si">{</span><span class="n">code</span><span class="si">}</span><span class="s1">: </span><span class="si">{</span><span class="n">message</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">code</span> <span class="o">=</span> <span class="n">code</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">message</span> <span class="o">=</span> <span class="n">message</span></div>


<span class="k">class</span> <span class="nc">Wrapper</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Wrapper implementation for use with the IB class.&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ib</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span> <span class="o">=</span> <span class="n">ib</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;ib_insync.wrapper&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timeoutHandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accountValues</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">AccountValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    (account, tag, currency, modelCode) -&gt; AccountValue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">acctSummary</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">tuple</span><span class="p">,</span> <span class="n">AccountValue</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    (account, tag, currency) -&gt; AccountValue</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">portfolio</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">PortfolioItem</span><span class="p">]]</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span>
        <span class="c1">#    account -&gt; conId -&gt; PortfolioItem</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">positions</span> <span class="o">=</span> <span class="n">defaultdict</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span>
        <span class="c1">#    account -&gt; conId -&gt; Position</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="n">OrderKeyType</span><span class="p">,</span> <span class="n">Trade</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    (client, orderId) or permId -&gt; Trade</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Trade</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    permId -&gt; Trade</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">fills</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">str</span><span class="p">,</span> <span class="n">Fill</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    execId -&gt; Fill</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">newsTicks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">NewsTick</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">msgId2NewsBulletin</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">NewsBulletin</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    msgId -&gt; NewsBulletin</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">tickers</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Ticker</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    id(Contract) -&gt; Ticker</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">:</span> <span class="n">Set</span><span class="p">[</span><span class="n">Ticker</span><span class="p">]</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Ticker</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    reqId -&gt; Ticker</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ticker2ReqId</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">],</span> <span class="n">Dict</span><span class="p">[</span><span class="n">Ticker</span><span class="p">,</span> <span class="nb">int</span><span class="p">]]</span> <span class="o">=</span> \
            <span class="n">defaultdict</span><span class="p">(</span><span class="nb">dict</span><span class="p">)</span>
        <span class="c1">#    tickType -&gt; Ticker -&gt; reqId</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#   reqId -&gt; marketDataType</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Any</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    live bars or live scan data</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2PnL</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">PnL</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    reqId -&gt; PnL</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2PnlSingle</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">PnLSingle</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    reqId -&gt; PnLSingle</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pnlKey2ReqId</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    (account, modelCode) -&gt; reqId</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pnlSingleKey2ReqId</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">tuple</span><span class="p">,</span> <span class="nb">int</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="c1">#    (account, modelCode, conId) -&gt; reqId</span>

        <span class="c1"># futures and results are linked by key:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="n">Any</span><span class="p">,</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="n">Any</span><span class="p">,</span> <span class="n">Any</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="c1"># UTC time of last network packet arrival:</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">:</span> <span class="n">datetime</span> <span class="o">=</span> <span class="kc">None</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="p">:</span> <span class="n">Dict</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="n">Contract</span><span class="p">]</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timeout</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setTimeout</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">setEventsDone</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Set all subscribtion-type events as done.&quot;&quot;&quot;</span>
        <span class="n">events</span> <span class="o">=</span> <span class="p">[</span><span class="n">ticker</span><span class="o">.</span><span class="n">updateEvent</span> <span class="k">for</span> <span class="n">ticker</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tickers</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span>
        <span class="n">events</span> <span class="o">+=</span> <span class="p">[</span><span class="n">sub</span><span class="o">.</span><span class="n">updateEvent</span> <span class="k">for</span> <span class="n">sub</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span>
        <span class="k">for</span> <span class="n">trade</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">events</span> <span class="o">+=</span> <span class="p">[</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">statusEvent</span><span class="p">,</span> <span class="n">trade</span><span class="o">.</span><span class="n">modifyEvent</span><span class="p">,</span> <span class="n">trade</span><span class="o">.</span><span class="n">fillEvent</span><span class="p">,</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">filledEvent</span><span class="p">,</span> <span class="n">trade</span><span class="o">.</span><span class="n">commissionReportEvent</span><span class="p">,</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">cancelEvent</span><span class="p">,</span> <span class="n">trade</span><span class="o">.</span><span class="n">cancelledEvent</span><span class="p">]</span>
        <span class="k">for</span> <span class="n">event</span> <span class="ow">in</span> <span class="n">events</span><span class="p">:</span>
            <span class="n">event</span><span class="o">.</span><span class="n">set_done</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">connectionClosed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">error</span> <span class="o">=</span> <span class="ne">ConnectionError</span><span class="p">(</span><span class="s1">&#39;Socket disconnect&#39;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">future</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">future</span><span class="o">.</span><span class="n">done</span><span class="p">():</span>
                <span class="n">future</span><span class="o">.</span><span class="n">set_exception</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="n">globalErrorEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">error</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">startReq</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">contract</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">container</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Start a new request and return the future that is associated</span>
<span class="sd">        with with the key and container. The container is a list by default.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">future</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">Future</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">future</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">container</span> <span class="k">if</span> <span class="n">container</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="k">else</span> <span class="p">[]</span>
        <span class="k">if</span> <span class="n">contract</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">contract</span>
        <span class="k">return</span> <span class="n">future</span>

    <span class="k">def</span> <span class="nf">_endReq</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">key</span><span class="p">,</span> <span class="n">result</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">success</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Finish the future of corresponding key with the given result.</span>
<span class="sd">        If no result is given then it will be popped of the general results.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">future</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">future</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">key</span><span class="p">,</span> <span class="p">[])</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">future</span><span class="o">.</span><span class="n">done</span><span class="p">():</span>
                <span class="k">if</span> <span class="n">success</span><span class="p">:</span>
                    <span class="n">future</span><span class="o">.</span><span class="n">set_result</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">future</span><span class="o">.</span><span class="n">set_exception</span><span class="p">(</span><span class="n">result</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">startTicker</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">]):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Start a tick request that has the reqId associated with the contract.</span>
<span class="sd">        Return the ticker.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">tickers</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">id</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="n">ticker</span> <span class="o">=</span> <span class="n">Ticker</span><span class="p">(</span>
                <span class="n">contract</span><span class="o">=</span><span class="n">contract</span><span class="p">,</span> <span class="n">ticks</span><span class="o">=</span><span class="p">[],</span> <span class="n">tickByTicks</span><span class="o">=</span><span class="p">[],</span>
                <span class="n">domBids</span><span class="o">=</span><span class="p">[],</span> <span class="n">domAsks</span><span class="o">=</span><span class="p">[],</span> <span class="n">domTicks</span><span class="o">=</span><span class="p">[])</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">tickers</span><span class="p">[</span><span class="nb">id</span><span class="p">(</span><span class="n">contract</span><span class="p">)]</span> <span class="o">=</span> <span class="n">ticker</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">ticker</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">contract</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ticker2ReqId</span><span class="p">[</span><span class="n">tickType</span><span class="p">][</span><span class="n">ticker</span><span class="p">]</span> <span class="o">=</span> <span class="n">reqId</span>
        <span class="k">return</span> <span class="n">ticker</span>

    <span class="k">def</span> <span class="nf">endTicker</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">ticker</span><span class="p">:</span> <span class="n">Ticker</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="n">Union</span><span class="p">[</span><span class="nb">int</span><span class="p">,</span> <span class="nb">str</span><span class="p">]):</span>
        <span class="n">reqId</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ticker2ReqId</span><span class="p">[</span><span class="n">tickType</span><span class="p">]</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">ticker</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">reqId</span>

    <span class="k">def</span> <span class="nf">startSubscription</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">subscriber</span><span class="p">,</span> <span class="n">contract</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Register a live subscription.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">contract</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">subscriber</span>

    <span class="k">def</span> <span class="nf">endSubscription</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">subscriber</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Unregister a live subscription.&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">subscriber</span><span class="o">.</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">subscriber</span><span class="o">.</span><span class="n">reqId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">orderKey</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">orderId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">permId</span><span class="p">:</span> <span class="nb">int</span><span class="p">)</span> <span class="o">-&gt;</span> \
            <span class="n">OrderKeyType</span><span class="p">:</span>
        <span class="n">key</span><span class="p">:</span> <span class="n">OrderKeyType</span>
        <span class="k">if</span> <span class="n">orderId</span> <span class="o">&lt;=</span> <span class="mi">0</span><span class="p">:</span>
            <span class="c1"># order is placed manually from TWS</span>
            <span class="n">key</span> <span class="o">=</span> <span class="n">permId</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">clientId</span><span class="p">,</span> <span class="n">orderId</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">key</span>

    <span class="k">def</span> <span class="nf">setTimeout</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timeout</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_timeoutHandle</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_timeoutHandle</span><span class="o">.</span><span class="n">cancel</span><span class="p">()</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timeoutHandle</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_timeout</span> <span class="o">=</span> <span class="n">timeout</span>
        <span class="k">if</span> <span class="n">timeout</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_setTimer</span><span class="p">(</span><span class="n">timeout</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">_setTimer</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">delay</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mi">0</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">now</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="n">diff</span> <span class="o">=</span> <span class="p">(</span><span class="n">now</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">)</span><span class="o">.</span><span class="n">total_seconds</span><span class="p">()</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">delay</span><span class="p">:</span>
            <span class="n">delay</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_timeout</span> <span class="o">-</span> <span class="n">diff</span>
        <span class="k">if</span> <span class="n">delay</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">loop</span> <span class="o">=</span> <span class="n">asyncio</span><span class="o">.</span><span class="n">get_event_loop</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_timeoutHandle</span> <span class="o">=</span> <span class="n">loop</span><span class="o">.</span><span class="n">call_later</span><span class="p">(</span><span class="n">delay</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_setTimer</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Timeout&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">setTimeout</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">timeoutEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">diff</span><span class="p">)</span>

    <span class="c1"># wrapper methods</span>

    <span class="k">def</span> <span class="nf">connectAck</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">nextValidId</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">managedAccounts</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">accountsList</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accounts</span> <span class="o">=</span> <span class="p">[</span><span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">accountsList</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">a</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">updateAccountTime</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">timestamp</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">updateAccountValue</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">tag</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">currency</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">currency</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="n">acctVal</span> <span class="o">=</span> <span class="n">AccountValue</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">currency</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accountValues</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">acctVal</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">accountValueEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">acctVal</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">accountDownloadEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_account</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="c1"># sent after updateAccountValue and updatePortfolio both finished</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;accountValues&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">accountUpdateMulti</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">tag</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">val</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">currency</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">currency</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="n">acctVal</span> <span class="o">=</span> <span class="n">AccountValue</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">val</span><span class="p">,</span> <span class="n">currency</span><span class="p">,</span> <span class="n">modelCode</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">accountValues</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">acctVal</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">accountValueEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">acctVal</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">accountUpdateMultiEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">accountSummary</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">_reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">tag</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">currency</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">currency</span><span class="p">)</span>
        <span class="n">acctVal</span> <span class="o">=</span> <span class="n">AccountValue</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">tag</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">currency</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">acctSummary</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">acctVal</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">accountSummaryEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">acctVal</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">accountSummaryEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updatePortfolio</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">posSize</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">marketPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">marketValue</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">averageCost</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">unrealizedPNL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">realizedPNL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
        <span class="n">portfItem</span> <span class="o">=</span> <span class="n">PortfolioItem</span><span class="p">(</span>
            <span class="n">contract</span><span class="p">,</span> <span class="n">posSize</span><span class="p">,</span> <span class="n">marketPrice</span><span class="p">,</span> <span class="n">marketValue</span><span class="p">,</span>
            <span class="n">averageCost</span><span class="p">,</span> <span class="n">unrealizedPNL</span><span class="p">,</span> <span class="n">realizedPNL</span><span class="p">,</span> <span class="n">account</span><span class="p">)</span>
        <span class="n">portfolioItems</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">portfolio</span><span class="p">[</span><span class="n">account</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">posSize</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">portfolioItems</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">portfolioItems</span><span class="p">[</span><span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">]</span> <span class="o">=</span> <span class="n">portfItem</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;updatePortfolio: </span><span class="si">{</span><span class="n">portfItem</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">updatePortfolioEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">portfItem</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">position</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">account</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">posSize</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">avgCost</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
        <span class="n">position</span> <span class="o">=</span> <span class="n">Position</span><span class="p">(</span><span class="n">account</span><span class="p">,</span> <span class="n">contract</span><span class="p">,</span> <span class="n">posSize</span><span class="p">,</span> <span class="n">avgCost</span><span class="p">)</span>
        <span class="n">positions</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">positions</span><span class="p">[</span><span class="n">account</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">posSize</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">positions</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">,</span> <span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">positions</span><span class="p">[</span><span class="n">contract</span><span class="o">.</span><span class="n">conId</span><span class="p">]</span> <span class="o">=</span> <span class="n">position</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;position: </span><span class="si">{</span><span class="n">position</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;positions&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">results</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">position</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">positionEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">position</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">positionEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;positions&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">pnl</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dailyPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">unrealizedPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">realizedPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">pnl</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2PnL</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pnl</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">pnl</span><span class="o">.</span><span class="n">dailyPnL</span> <span class="o">=</span> <span class="n">dailyPnL</span>
        <span class="n">pnl</span><span class="o">.</span><span class="n">unrealizedPnL</span> <span class="o">=</span> <span class="n">unrealizedPnL</span>
        <span class="n">pnl</span><span class="o">.</span><span class="n">realizedPnL</span> <span class="o">=</span> <span class="n">realizedPnL</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">pnlEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">pnl</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">pnlSingle</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">pos</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dailyPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">unrealizedPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">realizedPnL</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">pnlSingle</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2PnlSingle</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">pnlSingle</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">pnlSingle</span><span class="o">.</span><span class="n">position</span> <span class="o">=</span> <span class="n">pos</span>
        <span class="n">pnlSingle</span><span class="o">.</span><span class="n">dailyPnL</span> <span class="o">=</span> <span class="n">dailyPnL</span>
        <span class="n">pnlSingle</span><span class="o">.</span><span class="n">unrealizedPnL</span> <span class="o">=</span> <span class="n">unrealizedPnL</span>
        <span class="n">pnlSingle</span><span class="o">.</span><span class="n">realizedPnL</span> <span class="o">=</span> <span class="n">realizedPnL</span>
        <span class="n">pnlSingle</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">value</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">pnlSingleEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">pnlSingle</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">openOrder</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">orderId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">,</span>
            <span class="n">orderState</span><span class="p">:</span> <span class="n">OrderState</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This wrapper is called to:</span>

<span class="sd">        * feed in open orders at startup;</span>
<span class="sd">        * feed in open orders or order updates from other clients and TWS</span>
<span class="sd">          if clientId=master id;</span>
<span class="sd">        * feed in manual orders and order updates from TWS if clientId=0;</span>
<span class="sd">        * handle openOrders and allOpenOrders responses.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">whatIf</span><span class="p">:</span>
            <span class="c1"># response to whatIfOrder</span>
            <span class="k">if</span> <span class="n">orderState</span><span class="o">.</span><span class="n">initMarginChange</span> <span class="o">!=</span> <span class="nb">str</span><span class="p">(</span><span class="n">UNSET_DOUBLE</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span> <span class="n">orderState</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">orderKey</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span> <span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
            <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">permId</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">permId</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">totalQuantity</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">totalQuantity</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">lmtPrice</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">lmtPrice</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">auxPrice</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">auxPrice</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">order</span><span class="o">.</span><span class="n">orderType</span> <span class="o">=</span> <span class="n">order</span><span class="o">.</span><span class="n">orderType</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># ignore &#39;?&#39; values in the order</span>
                <span class="n">order</span> <span class="o">=</span> <span class="n">Order</span><span class="p">(</span><span class="o">**</span><span class="p">{</span>
                    <span class="n">k</span><span class="p">:</span> <span class="n">v</span> <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">order</span><span class="p">)</span><span class="o">.</span><span class="n">items</span><span class="p">()</span>
                    <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="s1">&#39;?&#39;</span><span class="p">})</span>
                <span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
                <span class="n">orderStatus</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="p">(</span>
                    <span class="n">orderId</span><span class="o">=</span><span class="n">orderId</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">orderState</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>
                <span class="n">trade</span> <span class="o">=</span> <span class="n">Trade</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">orderStatus</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[])</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">trade</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;openOrder: </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="o">.</span><span class="n">setdefault</span><span class="p">(</span><span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">,</span> <span class="n">trade</span><span class="p">)</span>
            <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s1">&#39;openOrders&#39;</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">results</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">openOrderEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># response to reqOpenOrders or reqAllOpenOrders</span>
                <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">order</span><span class="p">)</span>

        <span class="c1"># make sure that the client issues order ids larger then any</span>
        <span class="c1"># order id encountered (even from other clients) to avoid</span>
        <span class="c1"># &quot;Duplicate order id&quot; error</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">updateReqId</span><span class="p">(</span><span class="n">orderId</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">openOrderEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;openOrders&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">completedOrder</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">order</span><span class="p">:</span> <span class="n">Order</span><span class="p">,</span> <span class="n">orderState</span><span class="p">:</span> <span class="n">OrderState</span><span class="p">):</span>
        <span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
        <span class="n">orderStatus</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="p">(</span>
            <span class="n">orderId</span><span class="o">=</span><span class="n">order</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span> <span class="n">status</span><span class="o">=</span><span class="n">orderState</span><span class="o">.</span><span class="n">status</span><span class="p">)</span>
        <span class="n">trade</span> <span class="o">=</span> <span class="n">Trade</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">order</span><span class="p">,</span> <span class="n">orderStatus</span><span class="p">,</span> <span class="p">[],</span> <span class="p">[])</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="s1">&#39;completedOrders&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">order</span><span class="o">.</span><span class="n">permId</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">]</span> <span class="o">=</span> <span class="n">trade</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="p">[</span><span class="n">order</span><span class="o">.</span><span class="n">permId</span><span class="p">]</span> <span class="o">=</span> <span class="n">trade</span>

    <span class="k">def</span> <span class="nf">completedOrdersEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;completedOrders&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">orderStatus</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">orderId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">status</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">filled</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">remaining</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">avgFillPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">permId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">parentId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">lastFillPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">clientId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">whyHeld</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">mktCapPrice</span><span class="p">:</span> <span class="nb">float</span> <span class="o">=</span> <span class="mf">0.0</span><span class="p">):</span>
        <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">orderKey</span><span class="p">(</span><span class="n">clientId</span><span class="p">,</span> <span class="n">orderId</span><span class="p">,</span> <span class="n">permId</span><span class="p">)</span>
        <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
            <span class="n">msg</span><span class="p">:</span> <span class="n">Optional</span><span class="p">[</span><span class="nb">str</span><span class="p">]</span>
            <span class="n">oldStatus</span> <span class="o">=</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span>
            <span class="n">new</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span>
                <span class="n">status</span><span class="o">=</span><span class="n">status</span><span class="p">,</span> <span class="n">filled</span><span class="o">=</span><span class="n">filled</span><span class="p">,</span>
                <span class="n">remaining</span><span class="o">=</span><span class="n">remaining</span><span class="p">,</span> <span class="n">avgFillPrice</span><span class="o">=</span><span class="n">avgFillPrice</span><span class="p">,</span>
                <span class="n">permId</span><span class="o">=</span><span class="n">permId</span><span class="p">,</span> <span class="n">parentId</span><span class="o">=</span><span class="n">parentId</span><span class="p">,</span>
                <span class="n">lastFillPrice</span><span class="o">=</span><span class="n">lastFillPrice</span><span class="p">,</span> <span class="n">clientId</span><span class="o">=</span><span class="n">clientId</span><span class="p">,</span>
                <span class="n">whyHeld</span><span class="o">=</span><span class="n">whyHeld</span><span class="p">,</span> <span class="n">mktCapPrice</span><span class="o">=</span><span class="n">mktCapPrice</span><span class="p">)</span>
            <span class="n">curr</span> <span class="o">=</span> <span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="p">)</span>
            <span class="n">isChanged</span> <span class="o">=</span> <span class="n">curr</span> <span class="o">!=</span> <span class="p">{</span><span class="o">**</span><span class="n">curr</span><span class="p">,</span> <span class="o">**</span><span class="n">new</span><span class="p">}</span>
            <span class="k">if</span> <span class="n">isChanged</span><span class="p">:</span>
                <span class="n">dataclassUpdate</span><span class="p">(</span><span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="p">,</span> <span class="o">**</span><span class="n">new</span><span class="p">)</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="k">elif</span> <span class="p">(</span><span class="n">status</span> <span class="o">==</span> <span class="s1">&#39;Submitted&#39;</span> <span class="ow">and</span> <span class="n">trade</span><span class="o">.</span><span class="n">log</span>
                    <span class="ow">and</span> <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">message</span> <span class="o">==</span> <span class="s1">&#39;Modify&#39;</span><span class="p">):</span>
                <span class="c1"># order modifications are acknowledged</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="s1">&#39;Modified&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">msg</span> <span class="o">=</span> <span class="kc">None</span>

            <span class="k">if</span> <span class="n">msg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">msg</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logEntry</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;orderStatus: </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">orderStatusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">statusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">status</span> <span class="o">!=</span> <span class="n">oldStatus</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">status</span> <span class="o">==</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Filled</span><span class="p">:</span>
                        <span class="n">trade</span><span class="o">.</span><span class="n">filledEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">status</span> <span class="o">==</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Cancelled</span><span class="p">:</span>
                        <span class="n">trade</span><span class="o">.</span><span class="n">cancelledEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="s1">&#39;orderStatus: No order found for &#39;</span>
                <span class="s1">&#39;orderId </span><span class="si">%s</span><span class="s1"> and clientId </span><span class="si">%s</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">orderId</span><span class="p">,</span> <span class="n">clientId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">execDetails</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">contract</span><span class="p">:</span> <span class="n">Contract</span><span class="p">,</span> <span class="n">execution</span><span class="p">:</span> <span class="n">Execution</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This wrapper handles both live fills and responses to</span>
<span class="sd">        reqExecutions.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;execDetails </span><span class="si">{</span><span class="n">execution</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">execution</span><span class="o">.</span><span class="n">orderId</span> <span class="o">==</span> <span class="n">UNSET_INTEGER</span><span class="p">:</span>
            <span class="c1"># bug in TWS: executions of manual orders have unset value</span>
            <span class="n">execution</span><span class="o">.</span><span class="n">orderId</span> <span class="o">=</span> <span class="mi">0</span>
        <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">execution</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">trade</span><span class="p">:</span>
            <span class="n">key</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">orderKey</span><span class="p">(</span>
                <span class="n">execution</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">execution</span><span class="o">.</span><span class="n">orderId</span><span class="p">,</span> <span class="n">execution</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
            <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">key</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">trade</span> <span class="ow">and</span> <span class="n">contract</span> <span class="o">==</span> <span class="n">trade</span><span class="o">.</span><span class="n">contract</span><span class="p">:</span>
            <span class="n">contract</span> <span class="o">=</span> <span class="n">trade</span><span class="o">.</span><span class="n">contract</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">contract</span> <span class="o">=</span> <span class="n">Contract</span><span class="o">.</span><span class="n">create</span><span class="p">(</span><span class="o">**</span><span class="n">dataclassAsDict</span><span class="p">(</span><span class="n">contract</span><span class="p">))</span>
        <span class="n">execId</span> <span class="o">=</span> <span class="n">execution</span><span class="o">.</span><span class="n">execId</span>
        <span class="n">isLive</span> <span class="o">=</span> <span class="n">reqId</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span>
        <span class="n">time</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span> <span class="k">if</span> <span class="n">isLive</span> <span class="k">else</span> <span class="n">execution</span><span class="o">.</span><span class="n">time</span>
        <span class="n">fill</span> <span class="o">=</span> <span class="n">Fill</span><span class="p">(</span><span class="n">contract</span><span class="p">,</span> <span class="n">execution</span><span class="p">,</span> <span class="n">CommissionReport</span><span class="p">(),</span> <span class="n">time</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">execId</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">fills</span><span class="p">:</span>
            <span class="c1"># first time we see this execution so add it</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">fills</span><span class="p">[</span><span class="n">execId</span><span class="p">]</span> <span class="o">=</span> <span class="n">fill</span>
            <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">fills</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fill</span><span class="p">)</span>
                <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span>
                    <span class="n">time</span><span class="p">,</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span><span class="p">,</span>
                    <span class="sa">f</span><span class="s1">&#39;Fill </span><span class="si">{</span><span class="n">execution</span><span class="o">.</span><span class="n">shares</span><span class="si">}</span><span class="s1">@</span><span class="si">{</span><span class="n">execution</span><span class="o">.</span><span class="n">price</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logEntry</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">isLive</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;execDetails: </span><span class="si">{</span><span class="n">fill</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">execDetailsEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">,</span> <span class="n">fill</span><span class="p">)</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">fillEvent</span><span class="p">(</span><span class="n">trade</span><span class="p">,</span> <span class="n">fill</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">isLive</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fill</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">execDetailsEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">commissionReport</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">commissionReport</span><span class="p">:</span> <span class="n">CommissionReport</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">commissionReport</span><span class="o">.</span><span class="n">yield_</span> <span class="o">==</span> <span class="n">UNSET_DOUBLE</span><span class="p">:</span>
            <span class="n">commissionReport</span><span class="o">.</span><span class="n">yield_</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="k">if</span> <span class="n">commissionReport</span><span class="o">.</span><span class="n">realizedPNL</span> <span class="o">==</span> <span class="n">UNSET_DOUBLE</span><span class="p">:</span>
            <span class="n">commissionReport</span><span class="o">.</span><span class="n">realizedPNL</span> <span class="o">=</span> <span class="mf">0.0</span>
        <span class="n">fill</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">fills</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">commissionReport</span><span class="o">.</span><span class="n">execId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">fill</span><span class="p">:</span>
            <span class="n">report</span> <span class="o">=</span> <span class="n">dataclassUpdate</span><span class="p">(</span><span class="n">fill</span><span class="o">.</span><span class="n">commissionReport</span><span class="p">,</span> <span class="n">commissionReport</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;commissionReport: </span><span class="si">{</span><span class="n">report</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">permId2Trade</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">fill</span><span class="o">.</span><span class="n">execution</span><span class="o">.</span><span class="n">permId</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">trade</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">commissionReportEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">,</span> <span class="n">fill</span><span class="p">,</span> <span class="n">report</span><span class="p">)</span>
                <span class="n">trade</span><span class="o">.</span><span class="n">commissionReportEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">,</span> <span class="n">fill</span><span class="p">,</span> <span class="n">report</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># this is not a live execution and the order was filled</span>
                <span class="c1"># before this connection started</span>
                <span class="k">pass</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># commission report is not for this client</span>
            <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">orderBound</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">apiClientId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">apiOrderId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">contractDetails</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">contractDetails</span><span class="p">:</span> <span class="n">ContractDetails</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">contractDetails</span><span class="p">)</span>

    <span class="n">bondContractDetails</span> <span class="o">=</span> <span class="n">contractDetails</span>

    <span class="k">def</span> <span class="nf">contractDetailsEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">symbolSamples</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">contractDescriptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">ContractDescription</span><span class="p">]):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">contractDescriptions</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">marketRule</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">marketRuleId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">priceIncrements</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">PriceIncrement</span><span class="p">]):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;marketRule-</span><span class="si">{</span><span class="n">marketRuleId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="n">priceIncrements</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">marketDataType</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">marketDataId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span> <span class="o">=</span> <span class="n">marketDataId</span>

    <span class="k">def</span> <span class="nf">realtimeBar</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">open_</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">high</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">low</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">close</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">volume</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">wap</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">count</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="n">bar</span> <span class="o">=</span> <span class="n">RealTimeBar</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="n">open_</span><span class="p">,</span> <span class="n">high</span><span class="p">,</span> <span class="n">low</span><span class="p">,</span> <span class="n">close</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">wap</span><span class="p">,</span> <span class="n">count</span><span class="p">)</span>
        <span class="n">bars</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">bars</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">bars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">barUpdateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>
            <span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="kc">True</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">bar</span><span class="p">:</span> <span class="n">BarData</span><span class="p">):</span>
        <span class="n">results</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">results</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">bar</span><span class="o">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">parseIBDatetime</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">date</span><span class="p">)</span>  <span class="c1"># type: ignore</span>
            <span class="n">results</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalDataEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">_start</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">_end</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalDataUpdate</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">bar</span><span class="p">:</span> <span class="n">BarData</span><span class="p">):</span>
        <span class="n">bars</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">bars</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">bar</span><span class="o">.</span><span class="n">date</span> <span class="o">=</span> <span class="n">parseIBDatetime</span><span class="p">(</span><span class="n">bar</span><span class="o">.</span><span class="n">date</span><span class="p">)</span>  <span class="c1"># type: ignore</span>
        <span class="n">lastDate</span> <span class="o">=</span> <span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">date</span>
        <span class="k">if</span> <span class="n">bar</span><span class="o">.</span><span class="n">date</span> <span class="o">&lt;</span> <span class="n">lastDate</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="n">hasNewBar</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">bars</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">bar</span><span class="o">.</span><span class="n">date</span> <span class="o">&gt;</span> <span class="n">lastDate</span>
        <span class="k">if</span> <span class="n">hasNewBar</span><span class="p">:</span>
            <span class="n">bars</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">bar</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="n">bar</span><span class="p">:</span>
            <span class="n">bars</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">bar</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">barUpdateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="n">hasNewBar</span><span class="p">)</span>
        <span class="n">bars</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="n">hasNewBar</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">headTimestamp</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">headTimestamp</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">dt</span> <span class="o">=</span> <span class="n">parseIBDatetime</span><span class="p">(</span><span class="n">headTimestamp</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">dt</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">exc</span><span class="p">,</span> <span class="kc">False</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalTicks</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">ticks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">HistoricalTick</span><span class="p">],</span> <span class="n">done</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">+=</span> <span class="n">ticks</span>
        <span class="k">if</span> <span class="n">done</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalTicksBidAsk</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">ticks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">HistoricalTickBidAsk</span><span class="p">],</span> <span class="n">done</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">+=</span> <span class="n">ticks</span>
        <span class="k">if</span> <span class="n">done</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalTicksLast</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">ticks</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">HistoricalTickLast</span><span class="p">],</span> <span class="n">done</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">result</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">result</span> <span class="o">+=</span> <span class="n">ticks</span>
        <span class="k">if</span> <span class="n">done</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="c1"># additional wrapper method provided by Client</span>
    <span class="k">def</span> <span class="nf">priceSizeTick</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">price</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;priceSizeTick: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="c1"># https://interactivebrokers.github.io/tws-api/tick_types.html</span>
        <span class="k">if</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">66</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">price</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span> <span class="ow">and</span> <span class="n">size</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="k">if</span> <span class="n">price</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevBid</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span> <span class="o">=</span> <span class="n">price</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevBidSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">67</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">price</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span> <span class="ow">and</span> <span class="n">size</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="k">if</span> <span class="n">price</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevAsk</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span> <span class="o">=</span> <span class="n">price</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevAskSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">4</span><span class="p">,</span> <span class="mi">68</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">price</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevLast</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">last</span> <span class="o">=</span> <span class="n">price</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevLastSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">6</span><span class="p">,</span> <span class="mi">72</span><span class="p">):</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">high</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">7</span><span class="p">,</span> <span class="mi">73</span><span class="p">):</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">low</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">75</span><span class="p">):</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">close</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">14</span><span class="p">,</span> <span class="mi">76</span><span class="p">):</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">open</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">15</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">low13week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">16</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">high13week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">17</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">low26week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">18</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">high26week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">19</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">low52week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">20</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">high52week</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">35</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">auctionPrice</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">37</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">markPrice</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">50</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">bidYield</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">51</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">askYield</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">52</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">lastYield</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">if</span> <span class="n">price</span> <span class="ow">or</span> <span class="n">size</span><span class="p">:</span>
            <span class="n">tick</span> <span class="o">=</span> <span class="n">TickData</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">marketDataType</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickSize</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;tickSize: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="n">price</span> <span class="o">=</span> <span class="o">-</span><span class="mf">1.0</span>
        <span class="c1"># https://interactivebrokers.github.io/tws-api/tick_types.html</span>
        <span class="k">if</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">69</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="n">price</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevBidSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="mi">70</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">==</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span><span class="p">:</span>
                <span class="k">return</span>
            <span class="n">price</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevAskSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="mi">71</span><span class="p">):</span>
            <span class="n">price</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span>
            <span class="k">if</span> <span class="n">isNan</span><span class="p">(</span><span class="n">price</span><span class="p">):</span>
                <span class="k">return</span>
            <span class="k">if</span> <span class="n">size</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">prevLastSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">8</span><span class="p">,</span> <span class="mi">74</span><span class="p">):</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">volume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">21</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">avVolume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">27</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">callOpenInterest</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">28</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">putOpenInterest</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">29</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">callVolume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">30</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">putVolume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">34</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">auctionVolume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">36</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">auctionImbalance</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">61</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">regulatoryImbalance</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">86</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">futuresOpenInterest</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">87</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">avOptionVolume</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">89</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">shortableShares</span> <span class="o">=</span> <span class="n">size</span>
        <span class="k">if</span> <span class="n">price</span> <span class="ow">or</span> <span class="n">size</span><span class="p">:</span>
            <span class="n">tick</span> <span class="o">=</span> <span class="n">TickData</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">marketDataType</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2MarketDataType</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickSnapshotEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickByTickAllLast</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">price</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">size</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">tickAttribLast</span><span class="p">:</span> <span class="n">TickAttribLast</span><span class="p">,</span>
            <span class="n">exchange</span><span class="p">,</span> <span class="n">specialConditions</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;tickByTickAllLast: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">price</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevLast</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">last</span> <span class="o">=</span> <span class="n">price</span>
        <span class="k">if</span> <span class="n">size</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevLastSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span> <span class="o">=</span> <span class="n">size</span>
        <span class="n">tick</span> <span class="o">=</span> <span class="n">TickByTickAllLast</span><span class="p">(</span>
            <span class="n">tickType</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">tickAttribLast</span><span class="p">,</span>
            <span class="n">exchange</span><span class="p">,</span> <span class="n">specialConditions</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">tickByTicks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickByTickBidAsk</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">bidPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">askPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">bidSize</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">askSize</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">tickAttribBidAsk</span><span class="p">:</span> <span class="n">TickAttribBidAsk</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;tickByTickBidAsk: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">bidPrice</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevBid</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">bid</span> <span class="o">=</span> <span class="n">bidPrice</span>
        <span class="k">if</span> <span class="n">bidSize</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevBidSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">bidSize</span> <span class="o">=</span> <span class="n">bidSize</span>
        <span class="k">if</span> <span class="n">askPrice</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevAsk</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">ask</span> <span class="o">=</span> <span class="n">askPrice</span>
        <span class="k">if</span> <span class="n">askSize</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">prevAskSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">askSize</span> <span class="o">=</span> <span class="n">askSize</span>
        <span class="n">tick</span> <span class="o">=</span> <span class="n">TickByTickBidAsk</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">bidPrice</span><span class="p">,</span> <span class="n">askPrice</span><span class="p">,</span> <span class="n">bidSize</span><span class="p">,</span> <span class="n">askSize</span><span class="p">,</span>
            <span class="n">tickAttribBidAsk</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">tickByTicks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickByTickMidPoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">midPoint</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;tickByTickMidPoint: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="n">tick</span> <span class="o">=</span> <span class="n">TickByTickMidPoint</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">midPoint</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">tickByTicks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickString</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">47</span><span class="p">:</span>
                <span class="c1"># https://interactivebrokers.github.io/tws-api/fundamental_ratios_tags.html</span>
                <span class="n">d</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">t</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">)</span>                     <span class="c1"># type: ignore</span>
                         <span class="k">for</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;;&#39;</span><span class="p">)</span> <span class="k">if</span> <span class="n">t</span><span class="p">)</span>  <span class="c1"># type: ignore</span>
                <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
                    <span class="k">with</span> <span class="n">suppress</span><span class="p">(</span><span class="ne">ValueError</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">v</span> <span class="o">==</span> <span class="s1">&#39;-99999.99&#39;</span><span class="p">:</span>
                            <span class="n">v</span> <span class="o">=</span> <span class="s1">&#39;nan&#39;</span>
                        <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                        <span class="n">d</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="nb">int</span><span class="p">(</span><span class="n">v</span><span class="p">)</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">fundamentalRatios</span> <span class="o">=</span> <span class="n">FundamentalRatios</span><span class="p">(</span><span class="o">**</span><span class="n">d</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">48</span><span class="p">,</span> <span class="mi">77</span><span class="p">):</span>
                <span class="c1"># RT Volume or RT Trade Volume string format:</span>
                <span class="c1"># price;size;ms since epoch;total volume;VWAP;single trade</span>
                <span class="c1"># example:</span>
                <span class="c1"># 701.28;1;1348075471534;67854;701.46918464;true</span>
                <span class="n">priceStr</span><span class="p">,</span> <span class="n">sizeStr</span><span class="p">,</span> <span class="n">rtTime</span><span class="p">,</span> <span class="n">volume</span><span class="p">,</span> <span class="n">vwap</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;;&#39;</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">volume</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">48</span><span class="p">:</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">rtVolume</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">volume</span><span class="p">)</span>
                    <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">77</span><span class="p">:</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">rtTradeVolume</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">volume</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">vwap</span><span class="p">:</span>
                    <span class="n">ticker</span><span class="o">.</span><span class="n">vwap</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">vwap</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">rtTime</span><span class="p">:</span>
                    <span class="n">ticker</span><span class="o">.</span><span class="n">rtTime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span>
                        <span class="nb">int</span><span class="p">(</span><span class="n">rtTime</span><span class="p">)</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">priceStr</span> <span class="o">==</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                    <span class="k">return</span>
                <span class="n">price</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">priceStr</span><span class="p">)</span>
                <span class="n">size</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">sizeStr</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">price</span> <span class="ow">and</span> <span class="n">size</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">ticker</span><span class="o">.</span><span class="n">prevLast</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span><span class="p">:</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">prevLast</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">last</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">last</span> <span class="o">=</span> <span class="n">price</span>
                    <span class="k">if</span> <span class="n">ticker</span><span class="o">.</span><span class="n">prevLastSize</span> <span class="o">!=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span><span class="p">:</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">prevLastSize</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span>
                        <span class="n">ticker</span><span class="o">.</span><span class="n">lastSize</span> <span class="o">=</span> <span class="n">size</span>
                    <span class="n">tick</span> <span class="o">=</span> <span class="n">TickData</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
                    <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">59</span><span class="p">:</span>
                <span class="c1"># Dividend tick:</span>
                <span class="c1"># https://interactivebrokers.github.io/tws-api/tick_types.html#ib_dividends</span>
                <span class="c1"># example value: &#39;0.83,0.92,20130219,0.23&#39;</span>
                <span class="n">past12</span><span class="p">,</span> <span class="n">next12</span><span class="p">,</span> <span class="n">nextDate</span><span class="p">,</span> <span class="n">nextAmount</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">dividends</span> <span class="o">=</span> <span class="n">Dividends</span><span class="p">(</span>
                    <span class="nb">float</span><span class="p">(</span><span class="n">past12</span><span class="p">)</span> <span class="k">if</span> <span class="n">past12</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                    <span class="nb">float</span><span class="p">(</span><span class="n">next12</span><span class="p">)</span> <span class="k">if</span> <span class="n">next12</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                    <span class="n">parseIBDatetime</span><span class="p">(</span><span class="n">nextDate</span><span class="p">)</span> <span class="k">if</span> <span class="n">nextDate</span> <span class="k">else</span> <span class="kc">None</span><span class="p">,</span>
                    <span class="nb">float</span><span class="p">(</span><span class="n">nextAmount</span><span class="p">)</span> <span class="k">if</span> <span class="n">nextAmount</span> <span class="k">else</span> <span class="kc">None</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="sa">f</span><span class="s1">&#39;tickString with tickType </span><span class="si">{</span><span class="n">tickType</span><span class="si">}</span><span class="s1">: &#39;</span>
                <span class="sa">f</span><span class="s1">&#39;malformed value: </span><span class="si">{</span><span class="n">value</span><span class="si">!r}</span><span class="s1">&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickGeneric</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">value</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">value</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;genericTick: malformed value: </span><span class="si">{</span><span class="n">value</span><span class="si">!r}</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">23</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">histVolatility</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">24</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">impliedVolatility</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">31</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">indexFuturePremium</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">49</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">halted</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">54</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">tradeCount</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">55</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">tradeRate</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">56</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">volumeRate</span> <span class="o">=</span> <span class="n">value</span>
        <span class="k">elif</span> <span class="n">tickType</span> <span class="o">==</span> <span class="mi">58</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">rtHistVolatility</span> <span class="o">=</span> <span class="n">value</span>
        <span class="n">tick</span> <span class="o">=</span> <span class="n">TickData</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">tickType</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickReqParams</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">minTick</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">bboExchange</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">snapshotPermissions</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">mktDepthExchanges</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">depthMktDataDescriptions</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">DepthMktDataDescription</span><span class="p">]):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;mktDepthExchanges&#39;</span><span class="p">,</span> <span class="n">depthMktDataDescriptions</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updateMktDepth</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">position</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">operation</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">side</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">price</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">updateMktDepthL2</span><span class="p">(</span>
            <span class="n">reqId</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">side</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updateMktDepthL2</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">position</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">marketMaker</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">operation</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">side</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">price</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">size</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">isSmartDepth</span><span class="p">:</span> <span class="nb">bool</span> <span class="o">=</span> <span class="kc">False</span><span class="p">):</span>
        <span class="c1"># operation: 0 = insert, 1 = update, 2 = delete</span>
        <span class="c1"># side: 0 = ask, 1 = bid</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span>

        <span class="n">dom</span> <span class="o">=</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span> <span class="k">if</span> <span class="n">side</span> <span class="k">else</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span>
        <span class="k">if</span> <span class="n">operation</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">dom</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="n">position</span><span class="p">,</span> <span class="n">DOMLevel</span><span class="p">(</span><span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">marketMaker</span><span class="p">))</span>
        <span class="k">elif</span> <span class="n">operation</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
            <span class="n">dom</span><span class="p">[</span><span class="n">position</span><span class="p">]</span> <span class="o">=</span> <span class="n">DOMLevel</span><span class="p">(</span><span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">,</span> <span class="n">marketMaker</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">operation</span> <span class="o">==</span> <span class="mi">2</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">position</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">dom</span><span class="p">):</span>
                <span class="n">level</span> <span class="o">=</span> <span class="n">dom</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="n">position</span><span class="p">)</span>
                <span class="n">price</span> <span class="o">=</span> <span class="n">level</span><span class="o">.</span><span class="n">price</span>
                <span class="n">size</span> <span class="o">=</span> <span class="mi">0</span>

        <span class="n">tick</span> <span class="o">=</span> <span class="n">MktDepthData</span><span class="p">(</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">position</span><span class="p">,</span> <span class="n">marketMaker</span><span class="p">,</span> <span class="n">operation</span><span class="p">,</span> <span class="n">side</span><span class="p">,</span> <span class="n">price</span><span class="p">,</span> <span class="n">size</span><span class="p">)</span>
        <span class="n">ticker</span><span class="o">.</span><span class="n">domTicks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">tick</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickOptionComputation</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickAttrib</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">impliedVol</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">delta</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">optPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">pvDividend</span><span class="p">:</span>
            <span class="nb">float</span><span class="p">,</span> <span class="n">gamma</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">vega</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">theta</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span> <span class="n">undPrice</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="n">comp</span> <span class="o">=</span> <span class="n">OptionComputation</span><span class="p">(</span>
            <span class="n">tickAttrib</span><span class="p">,</span> <span class="n">impliedVol</span><span class="p">,</span> <span class="n">delta</span><span class="p">,</span> <span class="n">optPrice</span><span class="p">,</span> <span class="n">pvDividend</span><span class="p">,</span>
            <span class="n">gamma</span><span class="p">,</span> <span class="n">vega</span><span class="p">,</span> <span class="n">theta</span><span class="p">,</span> <span class="n">undPrice</span><span class="p">)</span>
        <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">ticker</span><span class="p">:</span>
            <span class="c1"># reply from reqMktData</span>
            <span class="c1"># https://interactivebrokers.github.io/tws-api/tick_types.html</span>
            <span class="k">if</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">80</span><span class="p">):</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">bidGreeks</span> <span class="o">=</span> <span class="n">comp</span>
            <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">11</span><span class="p">,</span> <span class="mi">81</span><span class="p">):</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">askGreeks</span> <span class="o">=</span> <span class="n">comp</span>
            <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">12</span><span class="p">,</span> <span class="mi">82</span><span class="p">):</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">lastGreeks</span> <span class="o">=</span> <span class="n">comp</span>
            <span class="k">elif</span> <span class="n">tickType</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">13</span><span class="p">,</span> <span class="mi">83</span><span class="p">):</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">modelGreeks</span> <span class="o">=</span> <span class="n">comp</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">reqId</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="p">:</span>
            <span class="c1"># reply from calculateImpliedVolatility or calculateOptionPrice</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">comp</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span>
                <span class="sa">f</span><span class="s1">&#39;tickOptionComputation: Unknown reqId: </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">deltaNeutralValidation</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">dnc</span><span class="p">:</span> <span class="n">DeltaNeutralContract</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">fundamentalData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">data</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">scannerParameters</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xml</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;scannerParams&#39;</span><span class="p">,</span> <span class="n">xml</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">scannerData</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">rank</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">contractDetails</span><span class="p">:</span> <span class="n">ContractDetails</span><span class="p">,</span>
            <span class="n">distance</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">benchmark</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">projection</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">legsStr</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">data</span> <span class="o">=</span> <span class="n">ScanData</span><span class="p">(</span>
            <span class="n">rank</span><span class="p">,</span> <span class="n">contractDetails</span><span class="p">,</span> <span class="n">distance</span><span class="p">,</span> <span class="n">benchmark</span><span class="p">,</span> <span class="n">projection</span><span class="p">,</span> <span class="n">legsStr</span><span class="p">)</span>
        <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataList</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataList</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">rank</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
                <span class="n">dataList</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
            <span class="n">dataList</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">data</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">scannerDataEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataList</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">dataList</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">scannerDataEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">dataList</span><span class="p">)</span>
            <span class="n">dataList</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">dataList</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">histogramData</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">items</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">HistogramData</span><span class="p">]):</span>
        <span class="n">result</span> <span class="o">=</span> <span class="p">[</span><span class="n">HistogramData</span><span class="p">(</span><span class="n">item</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="n">item</span><span class="o">.</span><span class="n">count</span><span class="p">)</span> <span class="k">for</span> <span class="n">item</span> <span class="ow">in</span> <span class="n">items</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">result</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">securityDefinitionOptionParameter</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">exchange</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span>
            <span class="n">tradingClass</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">multiplier</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">expirations</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">str</span><span class="p">],</span>
            <span class="n">strikes</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="nb">float</span><span class="p">]):</span>
        <span class="n">chain</span> <span class="o">=</span> <span class="n">OptionChain</span><span class="p">(</span>
            <span class="n">exchange</span><span class="p">,</span> <span class="n">underlyingConId</span><span class="p">,</span> <span class="n">tradingClass</span><span class="p">,</span> <span class="n">multiplier</span><span class="p">,</span>
            <span class="n">expirations</span><span class="p">,</span> <span class="n">strikes</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">chain</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">securityDefinitionOptionParameterEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">newsProviders</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">newsProviders</span><span class="p">:</span> <span class="n">List</span><span class="p">[</span><span class="n">NewsProvider</span><span class="p">]):</span>
        <span class="n">newsProviders</span> <span class="o">=</span> <span class="p">[</span>
            <span class="n">NewsProvider</span><span class="p">(</span><span class="n">code</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">code</span><span class="p">,</span> <span class="n">name</span><span class="o">=</span><span class="n">p</span><span class="o">.</span><span class="n">name</span><span class="p">)</span>
            <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">newsProviders</span><span class="p">]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;newsProviders&#39;</span><span class="p">,</span> <span class="n">newsProviders</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickNews</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">_reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">timeStamp</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">articleId</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">headline</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">extraData</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">news</span> <span class="o">=</span> <span class="n">NewsTick</span><span class="p">(</span>
            <span class="n">timeStamp</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">headline</span><span class="p">,</span> <span class="n">extraData</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">newsTicks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">news</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">tickNewsEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">news</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">newsArticle</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">articleType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">articleText</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">article</span> <span class="o">=</span> <span class="n">NewsArticle</span><span class="p">(</span><span class="n">articleType</span><span class="p">,</span> <span class="n">articleText</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">article</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalNews</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">articleId</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">headline</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="n">parseIBDatetime</span><span class="p">(</span><span class="n">time</span><span class="p">)</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="n">cast</span><span class="p">(</span><span class="n">datetime</span><span class="p">,</span> <span class="n">dt</span><span class="p">)</span>
        <span class="n">article</span> <span class="o">=</span> <span class="n">HistoricalNews</span><span class="p">(</span><span class="n">dt</span><span class="p">,</span> <span class="n">providerCode</span><span class="p">,</span> <span class="n">articleId</span><span class="p">,</span> <span class="n">headline</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_results</span><span class="p">[</span><span class="n">reqId</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">article</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">historicalNewsEnd</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">,</span> <span class="n">_hasMore</span><span class="p">:</span> <span class="nb">bool</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">updateNewsBulletin</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">msgId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">msgType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">message</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span>
            <span class="n">origExchange</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="n">bulletin</span> <span class="o">=</span> <span class="n">NewsBulletin</span><span class="p">(</span><span class="n">msgId</span><span class="p">,</span> <span class="n">msgType</span><span class="p">,</span> <span class="n">message</span><span class="p">,</span> <span class="n">origExchange</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">msgId2NewsBulletin</span><span class="p">[</span><span class="n">msgId</span><span class="p">]</span> <span class="o">=</span> <span class="n">bulletin</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">newsBulletinEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">bulletin</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">receiveFA</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">_faDataType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">faXmlData</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;requestFA&#39;</span><span class="p">,</span> <span class="n">faXmlData</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">currentTime</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">time</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span>
        <span class="n">dt</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">fromtimestamp</span><span class="p">(</span><span class="n">time</span><span class="p">,</span> <span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="s1">&#39;currentTime&#39;</span><span class="p">,</span> <span class="n">dt</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tickEFP</span><span class="p">(</span>
            <span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">tickType</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">basisPoints</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">formattedBasisPoints</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">totalDividends</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">holdDays</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">futureLastTradeDate</span><span class="p">:</span> <span class="nb">str</span><span class="p">,</span> <span class="n">dividendImpact</span><span class="p">:</span> <span class="nb">float</span><span class="p">,</span>
            <span class="n">dividendsToLastTradeDate</span><span class="p">:</span> <span class="nb">float</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">error</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">reqId</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">:</span> <span class="nb">int</span><span class="p">,</span> <span class="n">errorString</span><span class="p">:</span> <span class="nb">str</span><span class="p">):</span>
        <span class="c1"># https://interactivebrokers.github.io/tws-api/message_codes.html</span>
        <span class="n">warningCodes</span> <span class="o">=</span> <span class="p">{</span><span class="mi">165</span><span class="p">,</span> <span class="mi">202</span><span class="p">,</span> <span class="mi">399</span><span class="p">,</span> <span class="mi">404</span><span class="p">,</span> <span class="mi">434</span><span class="p">,</span> <span class="mi">492</span><span class="p">,</span> <span class="mi">10167</span><span class="p">}</span>
        <span class="n">isWarning</span> <span class="o">=</span> <span class="n">errorCode</span> <span class="ow">in</span> <span class="n">warningCodes</span> <span class="ow">or</span> <span class="mi">2100</span> <span class="o">&lt;=</span> <span class="n">errorCode</span> <span class="o">&lt;</span> <span class="mi">2200</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="p">(</span>
            <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="s2">&quot;Warning&quot;</span> <span class="k">if</span> <span class="n">isWarning</span> <span class="k">else</span> <span class="s2">&quot;Error&quot;</span><span class="si">}</span><span class="s1"> &#39;</span>
            <span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">errorCode</span><span class="si">}</span><span class="s1">, reqId </span><span class="si">{</span><span class="n">reqId</span><span class="si">}</span><span class="s1">: </span><span class="si">{</span><span class="n">errorString</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">contract</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_reqId2Contract</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">contract</span><span class="p">:</span>
            <span class="n">msg</span> <span class="o">+=</span> <span class="sa">f</span><span class="s1">&#39;, contract: </span><span class="si">{</span><span class="n">contract</span><span class="si">}</span><span class="s1">&#39;</span>

        <span class="k">if</span> <span class="n">isWarning</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">error</span><span class="p">(</span><span class="n">msg</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">reqId</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_futures</span><span class="p">:</span>
                <span class="c1"># the request failed</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">RaiseRequestErrors</span><span class="p">:</span>
                    <span class="n">error</span> <span class="o">=</span> <span class="n">RequestError</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">,</span> <span class="n">errorString</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">error</span><span class="p">,</span> <span class="n">success</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_endReq</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>

            <span class="k">elif</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="p">:</span>
                <span class="c1"># something is wrong with the order, cancel it</span>
                <span class="n">trade</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">trades</span><span class="p">[(</span><span class="bp">self</span><span class="o">.</span><span class="n">clientId</span><span class="p">,</span> <span class="n">reqId</span><span class="p">)]</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="n">trade</span><span class="o">.</span><span class="n">isDone</span><span class="p">():</span>
                    <span class="n">status</span> <span class="o">=</span> <span class="n">trade</span><span class="o">.</span><span class="n">orderStatus</span><span class="o">.</span><span class="n">status</span> <span class="o">=</span> <span class="n">OrderStatus</span><span class="o">.</span><span class="n">Cancelled</span>
                    <span class="n">logEntry</span> <span class="o">=</span> <span class="n">TradeLogEntry</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="n">status</span><span class="p">,</span> <span class="n">msg</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">)</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">log</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">logEntry</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">_logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;Canceled order: </span><span class="si">{</span><span class="n">trade</span><span class="si">}</span><span class="s1">&#39;</span><span class="p">)</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">orderStatusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">statusEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>
                    <span class="n">trade</span><span class="o">.</span><span class="n">cancelledEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">trade</span><span class="p">)</span>

        <span class="k">if</span> <span class="n">errorCode</span> <span class="o">==</span> <span class="mi">165</span><span class="p">:</span>
            <span class="c1"># for scan data subscription there are no longer matching results</span>
            <span class="n">dataList</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">dataList</span><span class="p">:</span>
                <span class="n">dataList</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
                <span class="n">dataList</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">dataList</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">errorCode</span> <span class="o">==</span> <span class="mi">317</span><span class="p">:</span>
            <span class="c1"># Market depth data has been RESET</span>
            <span class="n">ticker</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Ticker</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">ticker</span><span class="p">:</span>
                <span class="c1"># clear all DOM levels</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">domTicks</span> <span class="o">+=</span> <span class="p">[</span><span class="n">MktDepthData</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="n">level</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">level</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span><span class="p">]</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">domTicks</span> <span class="o">+=</span> <span class="p">[</span><span class="n">MktDepthData</span><span class="p">(</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="n">level</span><span class="o">.</span><span class="n">price</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
                    <span class="k">for</span> <span class="n">level</span> <span class="ow">in</span> <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span><span class="p">]</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">domAsks</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">domBids</span><span class="o">.</span><span class="n">clear</span><span class="p">()</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">errorCode</span> <span class="o">==</span> <span class="mi">10225</span><span class="p">:</span>
            <span class="c1"># Bust event occurred, current subscription is deactivated.</span>
            <span class="c1"># Please resubscribe real-time bars immediately</span>
            <span class="n">bars</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">reqId2Subscriber</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="n">RealTimeBarList</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelRealTimeBars</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqRealTimeBars</span><span class="p">(</span>
                    <span class="n">reqId</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">contract</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">barSize</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">whatToShow</span><span class="p">,</span>
                    <span class="n">bars</span><span class="o">.</span><span class="n">useRTH</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">realTimeBarsOptions</span><span class="p">)</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">bars</span><span class="p">,</span> <span class="n">BarDataList</span><span class="p">):</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">cancelHistoricalData</span><span class="p">(</span><span class="n">reqId</span><span class="p">)</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">client</span><span class="o">.</span><span class="n">reqHistoricalData</span><span class="p">(</span>
                    <span class="n">reqId</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">contract</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">endDateTime</span><span class="p">,</span>
                    <span class="n">bars</span><span class="o">.</span><span class="n">durationStr</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">barSizeSetting</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">whatToShow</span><span class="p">,</span>
                    <span class="n">bars</span><span class="o">.</span><span class="n">useRTH</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">formatDate</span><span class="p">,</span> <span class="n">bars</span><span class="o">.</span><span class="n">keepUpToDate</span><span class="p">,</span>
                    <span class="n">bars</span><span class="o">.</span><span class="n">chartOptions</span><span class="p">)</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">errorEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">reqId</span><span class="p">,</span> <span class="n">errorCode</span><span class="p">,</span> <span class="n">errorString</span><span class="p">,</span> <span class="n">contract</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">tcpDataArrived</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span> <span class="o">=</span> <span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(</span><span class="n">timezone</span><span class="o">.</span><span class="n">utc</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">ticker</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">:</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">ticks</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">tickByTicks</span> <span class="o">=</span> <span class="p">[]</span>
            <span class="n">ticker</span><span class="o">.</span><span class="n">domTicks</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span> <span class="o">=</span> <span class="nb">set</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">tcpDataProcessed</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">()</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">ticker</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">:</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">time</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">lastTime</span>
                <span class="n">ticker</span><span class="o">.</span><span class="n">updateEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="n">ticker</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ib</span><span class="o">.</span><span class="n">pendingTickersEvent</span><span class="o">.</span><span class="n">emit</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">pendingTickers</span><span class="p">)</span>
</pre></div>

           </div>
          </div>
          <footer>

  <hr/>

  <div role="contentinfo">
    <p>&#169; Copyright 2021, Ewald de Wit.</p>
  </div>

  Built with <a href="https://www.sphinx-doc.org/">Sphinx</a> using a
    <a href="https://github.com/readthedocs/sphinx_rtd_theme">theme</a>
    provided by <a href="https://readthedocs.org">Read the Docs</a>.
   

</footer>
        </div>
      </div>
    </section>
  </div>
  <script>
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script> 

</body>
</html>